//
//  CardDataViewController.m
//  LoveAttention
//
//  Created by 鞠超 on 15/12/31.
//  Copyright © 2015年 JC. All rights reserved.
//

#import "CardDataViewController.h"


#import "MJRefresh.h"
#import "DataProvider.h"
#import "SVProgressHUD.h"
#import "UIImageView+WebCache.h"
@interface CardDataViewController ()

@property (nonatomic, strong) UILabel * date_1;
@property (nonatomic, strong) UILabel * date_2;
@property (nonatomic, strong) UILabel * date_3;
@property (nonatomic, strong) UILabel * date_4;
@property (nonatomic, strong) UILabel * date_5;
@property (nonatomic, strong) UILabel * date_6;
@property (nonatomic, strong) UILabel * date_7;

@property (nonatomic, strong) UIView * view_1;
@property (nonatomic, strong) UIView * view_2;
@property (nonatomic, strong) UIView * view_3;
@property (nonatomic, strong) UIView * view_4;
@property (nonatomic, strong) UIView * view_5;
@property (nonatomic, strong) UIView * view_6;
@property (nonatomic, strong) UIView * view_7;

@property (nonatomic, strong) UILabel * label_1;
@property (nonatomic, strong) UILabel * label_2;
@property (nonatomic, strong) UILabel * label_3;
@property (nonatomic, strong) UILabel * label_4;
@property (nonatomic, strong) UILabel * label_5;
@property (nonatomic, strong) UILabel * label_6;

@property (nonatomic, strong) NSMutableArray * arr_date;

@property (nonatomic, strong) NSMutableArray * arr_num;

@end

@implementation CardDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navigationController];
    
    
    [self p_data];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - navigationController
- (void)p_navigationController
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.navigationItem.title = @"卡券数据分析";
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
        btn.frame = CGRectMake(10, 20, 25, 30);
    [btn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"刷新" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

}
//点击事件(左)
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBarButtonItemAction:(UIBarButtonItem *)bar
{
    NSLog(@"刷新");
    
    [self p_data1];
}

#pragma mark - 布局
- (void)p_setupView
{
    UIView * view_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 350)];
    view_bg.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view_bg];
    
    UIView * view_data = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 40, 300)];
//    view_data.backgroundColor = [UIColor orangeColor];
    [view_bg addSubview:view_data];
    
    self.label_1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 300 - 30 - 40, 40, 30)];
    self.label_1.text = @"5人";
    self.label_1.textAlignment = NSTextAlignmentLeft;
    self.label_1.font = [UIFont systemFontOfSize:13];
    [view_data addSubview:self.label_1];
    
    self.label_2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 300 - 30 - 40 *2, 40, 30)];
    self.label_2.text = @"10人";
    self.label_2.textAlignment = NSTextAlignmentLeft;
    self.label_2.font = [UIFont systemFontOfSize:13];
    [view_data addSubview:self.label_2];
    
    self.label_3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 300 - 30 - 40 * 3, 40, 30)];
    self.label_3.text = @"15人";
    self.label_3.textAlignment = NSTextAlignmentLeft;
    self.label_3.font = [UIFont systemFontOfSize:13];
    [view_data addSubview:self.label_3];
    
    self.label_4 = [[UILabel alloc] initWithFrame:CGRectMake(0, 300 - 30 - 40 * 4, 40, 30)];
    self.label_4.text = @"20人";
    self.label_4.textAlignment = NSTextAlignmentLeft;
    self.label_4.font = [UIFont systemFontOfSize:13];
    [view_data addSubview:self.label_4];
    
    self.label_5 = [[UILabel alloc] initWithFrame:CGRectMake(0, 300 - 30 - 40 * 5, 40, 30)];
    self.label_5.text = @"25人";
    self.label_5.textAlignment = NSTextAlignmentLeft;
    self.label_5.font = [UIFont systemFontOfSize:13];
    [view_data addSubview:self.label_5];
    
    self.label_6 = [[UILabel alloc] initWithFrame:CGRectMake(0, 300 - 30 - 40 * 6, 40, 30)];
    self.label_6.text = @"30人";
    self.label_6.textAlignment = NSTextAlignmentLeft;
    self.label_6.font = [UIFont systemFontOfSize:13];
    [view_data addSubview:self.label_6];
    
    CGFloat x = (self.view.frame.size.width - CGRectGetMaxX(view_data.frame) - 20) / 13;
    
    self.date_1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(view_data.frame) , CGRectGetMaxY(view_data.frame) + 55, x * 2, 20)];
    self.date_1.text = @"11.11";
//    self.date_1.backgroundColor = [UIColor redColor];
    self.date_1.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:self.date_1];
    
    self.date_2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_1.frame) , CGRectGetMaxY(view_data.frame) + 55, x * 2, 20)];
    self.date_2.text = @"11.12";
//    self.date_2.backgroundColor = [UIColor redColor];
    self.date_2.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:self.date_2];
    
    self.date_3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_2.frame) , CGRectGetMaxY(view_data.frame) + 55, x * 2, 20)];
    self.date_3.text = @"11.13";
//    self.date_3.backgroundColor = [UIColor redColor];
    self.date_3.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:self.date_3];
    
    self.date_4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_3.frame) , CGRectGetMaxY(view_data.frame) + 55, x * 2, 20)];
    self.date_4.text = @"11.14";
//    self.date_4.backgroundColor = [UIColor redColor];
    self.date_4.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:self.date_4];
    
    self.date_5 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_4.frame) , CGRectGetMaxY(view_data.frame) + 55, x * 2, 20)];
    self.date_5.text = @"11.15";
//    self.date_5.backgroundColor = [UIColor redColor];
    self.date_5.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:self.date_5];
    
    self.date_6 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_5.frame) , CGRectGetMaxY(view_data.frame) + 55, x * 2, 20)];
    self.date_6.text = @"11.16";
//    self.date_6.backgroundColor = [UIColor redColor];
    self.date_6.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:self.date_6];
    
    self.date_7 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_6.frame) , CGRectGetMaxY(view_data.frame) + 55, x * 2, 20)];
    self.date_7.text = @"11.17";
//    self.date_7.backgroundColor = [UIColor redColor];
    self.date_7.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:self.date_7];
    
    self.view_1 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(view_data.frame) + 5, CGRectGetMaxY(view_data.frame) + 35  - [self.arr_num[0] integerValue] * 7, x , [self.arr_num[0] integerValue] * 7)];
    self.view_1.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.view_1];
    
    
    self.view_2 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_1.frame) + 5, CGRectGetMaxY(view_data.frame) + 35 - [self.arr_num[1] integerValue] * 7, x , [self.arr_num[1] integerValue] * 7)];
    self.view_2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.view_2];
    
    
    self.view_3 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_2.frame) + 5, CGRectGetMaxY(view_data.frame) + 35  - [self.arr_num[2] integerValue] * 7, x , [self.arr_num[2] integerValue] * 7)];
    self.view_3.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.view_3];
    
    
    self.view_4 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_3.frame) + 5, CGRectGetMaxY(view_data.frame) + 35  - [self.arr_num[3] integerValue] * 7, x , [self.arr_num[3] integerValue] * 7)];
    self.view_4.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.view_4];
    
    
    self.view_5 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_4.frame) + 5, CGRectGetMaxY(view_data.frame) + 35  - [self.arr_num[4] integerValue] * 7, x , [self.arr_num[4] integerValue] * 7)];
    self.view_5.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.view_5];
    
    
    self.view_6 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_5.frame) + 5, CGRectGetMaxY(view_data.frame) + 35  - [self.arr_num[5] integerValue] * 7, x , [self.arr_num[5] integerValue] * 7)];
    self.view_6.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.view_6];
    
    
    self.view_7 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.date_6.frame) + 5, CGRectGetMaxY(view_data.frame) + 35  - [self.arr_num[6] integerValue] * 7, x , [self.arr_num[6] integerValue] * 7)];
    self.view_7.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.view_7];
    
    
    self.date_1.text = self.arr_date[0];
    self.date_2.text = self.arr_date[1];
    self.date_3.text = self.arr_date[2];
    self.date_4.text = self.arr_date[3];
    self.date_5.text = self.arr_date[4];
    self.date_6.text = self.arr_date[5];
    self.date_7.text = self.arr_date[6];
}

#pragma mark - 数据
- (void)p_data
{
    DataProvider * dataprovider = [[DataProvider alloc] init];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [dataprovider setDelegateObject:self setBackFunctionName:@"CardData:"];
    
    [dataprovider CardDataWith:[defaults objectForKey:@"merchantid"]];
}

- (void)CardData:(id )dict
{
        NSLog(@"%@",dict);
    
    self.arr_num = nil;
    self.arr_num = nil;
    
    if ([dict[@"status"][@"succeed"] intValue] == 1) {
        @try
        {
            
            for (NSDictionary * dic in dict[@"data"]) {
                
                NSString * str = [[NSString stringWithFormat:@"%@",dic[@"date"]] substringFromIndex:4];
                NSString * str_month = [str substringToIndex:2];
                NSString * str_day = [str substringFromIndex:2];
                NSString * str_date = [NSString stringWithFormat:@"%@.%@",str_month,str_day];
                
                [self.arr_date addObject:str_date];
                
                
                [self.arr_num addObject:dic[@"num"]];
                
            }
            
            
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
            [self p_setupView];

        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }
}


- (void)p_data1
{
    DataProvider * dataprovider = [[DataProvider alloc] init];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [dataprovider setDelegateObject:self setBackFunctionName:@"CardData1:"];
    
    [dataprovider CardDataWith:[defaults objectForKey:@"merchantid"]];
}

- (void)CardData1:(id )dict
{
    NSLog(@"%@",dict);
    
    self.arr_num = nil;
    self.arr_num = nil;
    
    if ([dict[@"status"][@"succeed"] intValue] == 1) {
        @try
        {
            [SVProgressHUD showSuccessWithStatus:@"刷新成功" maskType:(SVProgressHUDMaskTypeBlack)];
            
            for (NSDictionary * dic in dict[@"data"]) {
                
                NSString * str = [[NSString stringWithFormat:@"%@",dic[@"date"]] substringFromIndex:4];
                NSString * str_month = [str substringToIndex:2];
                NSString * str_day = [str substringFromIndex:2];
                NSString * str_date = [NSString stringWithFormat:@"%@.%@",str_month,str_day];
                
                [self.arr_date addObject:str_date];
                
                [self.arr_num addObject:dic[@"num"]];
                
            }
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
            [self p_setupView];
            
        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }
}


#pragma mark - 懒加载
- (NSMutableArray *)arr_date
{
    if(_arr_date == nil)
    {
        self.arr_date = [NSMutableArray array];
    }
    return _arr_date;
}

- (NSMutableArray *)arr_num
{
    if(_arr_num == nil)
    {
        self.arr_num = [NSMutableArray array];
    }
    return _arr_num;
}




@end
